package cn.jimmie.learn.viewArt.ball;

import android.app.Activity;
import android.content.ActivityNotFoundException;

/**
 * function : 悬浮球管理类
 * 仅包可见
 *
 * <p> Create Time: 2018年12月31日 09:16 </p>
 * <p> @author Jimmie.Qian  </p>
 */
class AssistBallProxy implements IProxy, AssistLifeCycleManger.OnLifeCycleCallbacks,
        AssistScrollerView.OnEventListener {
    private AssistLifeCycleManger mLifeCycle;
    private AssistScrollerView mBall;
    private AssistGCView mGC;
    private AssistPopView mPop;

    private AssistBall.OnAssistBallEventListener mBallListener;

    int getBallX() {
        return mBall.lx();
    }

    int getBallY() {
        return mBall.ly();
    }

    AssistBallProxy(AssistBall.Builder builder) {
        Activity activity = builder.activity;
        if (activity == null || activity.isFinishing()) {
            throw new ActivityNotFoundException("activity is null or finished,can not create for activity invalid");
        }
        mLifeCycle = new AssistLifeCycleManger(activity);
        mBall = AssistBallView
                .creator(activity)
                .setLogoStyle(builder.logoStyle)
                .setBallPosition(builder.position)
                .setPingEdges(builder.edgeInNormalWarning, builder.edgeInWarningDanger)
                .create();
        mGC = AssistGCView.creator(activity).create();
        mPop = new AssistPopView(activity);
    }

    @Override
    public void attacheToWindow() {
        if (mBall.isAttached()) return;
        mBall.restore();
        mLifeCycle.register(this);
        mBall.setListener(this);
        mBall.attachToWindow();
        if (mBallListener != null)
            mBallListener.onBallAttached();
    }

    @Override
    public void detachFromWindow() {
        detachFromWindow(false);
    }

    private void detachFromWindow(boolean fromGC) {
        mBall.setListener(null);
        if (mBallListener != null)
            mBallListener.onBallDetached();
        if (mBall.isAttached()) mBall.save(fromGC);
        mGC.detachFromWindow();
        mBall.detachFromWindow();
        mPop.detachFromWindow();
        mLifeCycle.unregister();
    }

    @Override
    public boolean isAttached() {
        return mBall.isAttached();
    }

    @Override
    public void addMessage(AssistBall.Message message, AssistBall.OnAssistPopEventListener listener) {
        if (!isAttached()) return;
        mPop.addMessage(message, listener);
        onAssistViewScrollOver();
    }

    @Override
    public void addMessage(AssistBall.Message message) {
        addMessage(message, null);
    }

    @Override
    public void updatePing(int ping) {
        if (!isAttached()) return;
        mBall.setPing(ping);
    }

    @Override
    public void setRedDot(boolean visibility) {
        if (!isAttached()) return;
        mBall.setRedDot(visibility);
    }

    @Override
    public void setBallEventListener(AssistBall.OnAssistBallEventListener listener) {
        mBallListener = listener;
    }

    @Override
    public void onActivityStart(Activity activity) {
//        if (isBallAttached) attacheToWindow();
    }

    @Override
    public void onActivityStop(Activity activity) {
//        isBallAttached = isAttached();
//        if (mBallListener != null && mBall.isAttached())
//            mBallListener.onBallDetached();
//        mBall.detachFromWindow();
    }

    @Override
    public void onActivityDestroy(Activity activity) {
        detachFromWindow();
    }

    /**
     * 屏幕尺寸变化时被调用
     */
    @Override
    public void onLayoutChanged(int width, int height) {
        mPop.onLayoutChanged(width, height);
        mBall.onLayoutChanged(width, height);
        mGC.onLayoutChanged(width, height);
    }

    @Override
    public void onAssistViewClick(boolean lock) {
        if (mBallListener != null) mBallListener.onBallClick(lock);
    }

    @Override
    public void onAssistViewLongClick(boolean lock) {
        if (mBallListener != null) mBallListener.onBallLongClick(lock);
    }

    @Override
    public void onAssistViewActionMove(int x, int y) {
        mPop.detachFromWindow();
        mGC.attachToWindow();
        mGC.checkHit(x, y);
        if (mBallListener != null) mBallListener.onBallActionMove(x, y);
    }

    @Override
    public void onAssistViewActionUp() {
        if (mGC.isHit()) {
            detachFromWindow(true);
        } else mGC.detachFromWindow();
    }

    @Override
    public void onAssistViewScrollOver() {
        int x = mBall.lx();
        int y = mBall.ly();
        int s = mBall.getSize();
        mPop.attachToWindow(x, y, s);
    }

    @Override
    public void onAssistViewAttachedEdge() {
        if (mBallListener != null) mBallListener.onBallAttachedEdge();
    }
}
